package com.wingstudio.springcloud.controller;

import com.wingstudio.springcloud.result.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import com.wingstudio.springcloud.entities.Payment;
import com.wingstudio.springcloud.service.Impl.PaymentServiceImpl;

import java.util.HashMap;


/**
 * @Author ITcz
 * @Data 2020-12-07 - 22:05
 */

@RestController
@RequestMapping("/payment")
public class PaymentController {

    private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(PaymentController.class);

    @Autowired
    private PaymentServiceImpl paymentService;

    @Value("${server.port}")
    private String serverPort;

    @PostMapping("/create" )
    public R create(@RequestBody Payment payment){
        int result = paymentService.create(payment);
        log.info("******插入结果" + payment);
        if(result > 0) {
            return R.ok("插入成功:serverPort: " + serverPort);
        }else {
            return R.error("插入失败");
        }
    }

    @GetMapping("/get/{id}")
    public R getPaymentById(@PathVariable("id") Integer id){
        Payment payment = paymentService.getPaymentById(id);
        log.info("******获取结果" + payment);
        if(payment !=  null) {
            HashMap res = new HashMap();
            res.put("payment", payment);
            res.put("serverPort: ", serverPort);
            return R.ok(res);
        }else {
            return R.error(444, "没有对应记录，查询ID："+id);
        }
    }

    @GetMapping("/lb")
    public String getPaymentLB(){
        return serverPort;
    }

}
